﻿using Microsoft.Practices.ServiceLocation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Youtube_HD.Model;
using Youtube_HD.Services;
using Youtube_HD.View;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Youtube_HD.Controls
{
    public sealed partial class ControlPlaylistSearch : UserControl
    {
        static bool isLoading = true;
        public ControlPlaylistSearch()
        {
            this.InitializeComponent();
            this.Loaded += ControlPlaylistSearchData;

        }

        void ControlPlaylistSearchData(object sender, RoutedEventArgs e)
        {
            DetectScrollState temp = new DetectScrollState(this.listData, true);
            temp.ScrollingDownToEnd += temp_ScrollingDownToEndData;

        }
        private ISearchViewModel searchViewModel
        {
            get { return ServiceLocator.Current.GetInstance<ISearchViewModel>(); }
        }
        private async void temp_ScrollingDownToEndData()
        {
            if (isLoading)
            {
                isLoading = false;
                await searchViewModel.YoutubePlaylistSearch();
                isLoading = true;
            }
        }

      

        private void Playlist_ItemClick(object sender, ItemClickEventArgs e)
        {
            var data = (YoutubeVideo)e.ClickedItem;
            if (!string.IsNullOrEmpty(data.PlayListId))
            {
                Frame fm = Window.Current.Content as Frame;
                fm.Navigate(typeof(DetailPlayList), data);
            }
            else
            {
                new MessageDialog("Play List not found!").ShowAsync();
            }
        }
    }
}
